package com.lyf.boot.modules.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lyf.boot.base.system.entity.SysPerm;
import com.lyf.boot.constant.Constant;
import com.lyf.boot.modules.index.entity.MenuTreeVo;
import com.lyf.boot.modules.system.dao.SysPermDao;
import com.lyf.boot.modules.system.entity.dto.SysPermDto;
import com.lyf.boot.modules.system.entity.vo.SysPermVo;
import com.lyf.boot.modules.system.service.ISysPermService;
import com.lyf.boot.utils.AuthUtil;
import com.lyf.boot.utils.MapstructUtil;
import com.lyf.boot.utils.TreeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Luyufan
 * @date 2023/7/6 11:36
 */
@Service
@RequiredArgsConstructor
public class SysPermServiceImpl implements ISysPermService {

    private final static String ID_NAME = "permId";

    private final SysPermDao sysPermDao;

    @Override
    public List<SysPermVo> getSysPermList() {
        List<SysPermVo> list = sysPermDao.selectSysPermList();
        return TreeUtil.buildTree(list, Constant.TREE_PARENT_ID, ID_NAME, Constant.PARENT_ID_NAME, Constant.CHILDREN_NAME);
    }

    @Override
    public List<MenuTreeVo> getUserPerm() {
        List<SysPerm> perms = sysPermDao.selectUserPerm(AuthUtil.getUserId());
        List<MenuTreeVo> menuTree = new ArrayList<>();
        for (SysPerm perm : perms) {
            MenuTreeVo menuTreeVo = MapstructUtil.convert(perm, MenuTreeVo.class);
            MenuTreeVo.Meta meta = new MenuTreeVo.Meta();
            meta.setOrder(perm.getSort());
            meta.setTitle(perm.getPermName());
            meta.setIcon(perm.getIcon());
            meta.setKeepAlive(ObjectUtil.equals(perm.getKeepAlive(), 0));
            meta.setAffixTab(ObjectUtil.equals(perm.getAffixTab(), 1));
            meta.setAffixTabOrder(perm.getAffixTabOrder());
            meta.setAuthority(CollUtil.toList(perm.getPermCode()));
            meta.setOpenInNewWindow(ObjectUtil.equals(perm.getOpenInNewWindow(), 1));
            boolean basicLayout = StrUtil.equals(perm.getComponent(), "BasicLayout") || (ObjUtil.equals(perm.getParentId(), 0L) && ObjUtil.equals(perm.getType(),1));
            meta.setNoBasicLayout(!basicLayout);
            if(Objects.equals(perm.getLinkType(), 1)){
                meta.setIframeSrc(perm.getIframeSrc());
            }else if(Objects.equals(perm.getLinkType(), 2)){
                meta.setLink(perm.getLink());
            }
            menuTreeVo.setMeta(meta);
            menuTree.add(menuTreeVo);
        }
        return TreeUtil.buildTree(menuTree, Constant.TREE_PARENT_ID, ID_NAME, Constant.PARENT_ID_NAME, Constant.CHILDREN_NAME);
    }

    @Override
    public SysPermVo getSysPermInfo(Long menuId) {
        return sysPermDao.selectSysPermById(menuId);
    }

    @Override
    public void saveSysPerm(SysPermDto dto) {
        if(dto.getParentId() != null){
            boolean exists = sysPermDao.exists(Wrappers.<SysPerm>lambdaQuery().eq(SysPerm::getPermId, dto.getParentId()));
            Assert.isTrue(exists, "上级权限不存在");
        }
        SysPerm sysPerm = MapstructUtil.convert(dto, SysPerm.class);
        if(ObjectUtil.notEqual(dto.getLinkType(), 0)){
            dto.setComponent("IFrameView");
        }
        sysPerm.setParentId(ObjectUtil.defaultIfNull(sysPerm.getParentId(), Constant.TREE_PARENT_ID));
        sysPermDao.insert(sysPerm);
    }

    @Override
    public void updateSysPerm(SysPermDto dto) {
        if(dto.getParentId() != null){
            boolean exists = sysPermDao.exists(Wrappers.<SysPerm>lambdaQuery().eq(SysPerm::getPermId, dto.getParentId()));
            Assert.isTrue(exists, "上级权限不存在");
        }
        if(ObjectUtil.notEqual(dto.getLinkType(), 0)){
            dto.setComponent("IFrameView");
        }
        SysPerm sysPerm = MapstructUtil.convert(dto, SysPerm.class);
        sysPerm.setParentId(ObjectUtil.defaultIfNull(sysPerm.getParentId(), Constant.TREE_PARENT_ID));
        sysPermDao.updateById(sysPerm);
    }

    @Override
    public void deleteSysPerm(List<Long> ids) {
        //判断是否有下级权限
        boolean exists = sysPermDao.exists(Wrappers.<SysPerm>lambdaQuery().in(SysPerm::getParentId, ids));
        Assert.isFalse(exists, "存在下级权限，请先删除下级权限");
        sysPermDao.deleteByIds(ids);
    }

    @Override
    public List<String> getUserPermCodes() {
        return sysPermDao.selectUserPermCodes(AuthUtil.getUserId());
    }

}
